package com.xs.service.impl;

import com.alibaba.excel.EasyExcel;
import com.xs.dto.UserAddress;
import com.xs.dto.UserGenderDto;
import com.xs.entity.User;
import com.xs.listener.UserListener;
import com.xs.mapper.UserMapper;
import com.xs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service // 1、业务逻辑接口实现类
public class UserServiceImpl implements UserService {

    @Autowired // 2、自动接线数据访问层
    private UserMapper userMapper;

    @Override // 3、重写业务逻辑层的查询所有用户方法
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override // 4、添加用户
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override // 5、修改用户
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override // 6、删除用户
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override // 7、批量删除
    public void deleteByIds(Integer[] ids) {
        userMapper.deleteByIds(ids);
    }

    @Override // 8、导出Excel
    public List<User> findByIds(Integer[] ids) {
        return userMapper.findByIds(ids);
    }

    @Override // 9、导入Excel
    public void readExcel(MultipartFile file) {
        // 9.1 获取输入流对象
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 9.2 读取、导入 用流读取
        EasyExcel.read(inputStream, User.class, new UserListener(userMapper)).sheet().doRead();
    }

    @Override // 10、获取男女人数
    public List<UserGenderDto> findGender() {
        return userMapper.findGender();
    }

    @Override // 11、获取用户地址以及人数
    public List<UserAddress> findAddress() {
        return userMapper.findAddress();
    }
}
